<?php
//引入运程方案
function addhauldeploy($hid,$h_wmtid,$type,$isdelete,$address,$linkman,$linkman_tel,$dt)
{
	$_haul_mod =& m('haul');
	$db=&db();
	if($isdelete===true){
		$sql="delete from zys_haul_collect where exists(select 1 from zys_haul where hc_h_tid=h_tid and h_wmtid=".$h_wmtid.")";
		$db->query($sql);

		$sql="delete from zys_haul where h_wmtid=".$h_wmtid;
		$db->query($sql);
	}
	$sql="SELECT * FROM zys_haul_define WHERE h_zhdmtid='".$hid."'";
	$temp=$db->getAll($sql);

	if($temp){
		foreach ($temp as $v){
			$data['h_wmtid'] = $h_wmtid;
			$data['h_name'] = $v['h_name'];
			$data['h_short_name'] = $v['h_short_name'];

			$data['h_order'] = $v['h_order'];
			if($type=='疏港'){
				if($v['h_linkman'] == '@TI_linkman' || $v['h_linkman'] == '@TI_container_man' || $v['h_linkman'] == '@TI_full_container_man' || $v['h_linkman_tel']=='@TI_tel' ||
				$v['h_linkman_tel']=='@TI_container_tel' || $v['h_linkman_tel']=='@TI_full_container_tel' || $v['h_address'] == '@TI_unload' || $v['h_address'] == '@TI_container_depot' || $v['h_address'] == '@TI_full_container_depot' || $v['h_date'] == '@TI_unload_time' || $v['h_date'] == '@TI_collect_time'){
					//查数据库
					$sql = "select gt.g_container_man,gt.g_container_tel,gt.g_container_depot,g_container_lng,g_container_lat,gt.g_full_container_man,gt.g_full_container_tel,gt.g_full_container_depot,g_full_container_lng,g_full_container_lat,gt.g_loading_place,gt.g_loading_lng,gt.g_loading_lat,gt.g_loading_man,gt.g_loading_tel,gt.g_packingtime,gt.g_collectiontime from zys_goods_trunk as gt left join zys_waybill_sub_trunk as wst on gt.g_tid= wst.ws_gid where wst.ws_wmtid=".$h_wmtid." GROUP BY ws_gid";
					$row = $db->getrow($sql);
					if($v['h_linkman'] == '@TI_linkman'){
						$data['h_linkman'] = $row['g_loading_man'];
					}
					elseif($v['h_linkman'] == '@TI_container_man'){

						$data['h_linkman'] = $row['g_container_man'];
					}
					elseif($v['h_linkman'] == '@TI_full_container_man'){

						$data['h_linkman'] = $row['g_full_container_man'];
					}
					else{
						$data['h_linkman'] = $v['h_linkman'];
					}

					if($v['h_linkman_tel']=='@TI_tel'){

						$data['h_linkman_tel'] = $row['g_loading_tel'];

					}elseif($v['h_linkman_tel']=='@TI_container_tel'){

						$data['h_linkman_tel'] = $row['g_container_tel'];

					}elseif($v['h_linkman_tel']=='@TI_full_container_tel'){

						$data['h_linkman_tel'] = $row['g_full_container_tel'];
					}else{

						$data['h_linkman_tel'] = $v['h_linkman_tel'];
					}

					if($v['h_address'] == '@TI_unload'){
						$data['h_address'] = $row['g_loading_place'];
						$data['h_lng'] = $row['g_loading_lng'];
						$data['h_lat'] = $row['g_loading_lat'];
					}elseif($v['h_address'] == '@TI_container_depot'){
						$data['h_address'] = $row['g_container_depot'];
						$data['h_lng'] = $row['g_container_lng'];
						$data['h_lat'] = $row['g_container_lat'];
					}elseif($v['h_address'] == '@TI_full_container_depot'){
						$data['h_address'] = $row['g_full_container_depot'];
						$data['h_lng'] = $row['g_full_container_lng'];
						$data['h_lat'] = $row['g_full_container_lat'];
					}else{
						$data['h_address'] = $v['h_address'];
					}
					if($v['h_date'] == '@TI_unload_time'){
						$data['h_required_service_date'] = $row['g_packingtime'];
					}elseif($v['h_date'] == '@TI_collect_time'){
						$data['h_required_service_date'] = $row['g_collectiontime'];
					}else{
						$data['h_required_service_date'] = $v['h_date'];
					}
				}else{
					$data['h_linkman'] = $v['h_linkman'];
					$data['h_linkman_tel'] = $v['h_linkman_tel'];
					$data['h_address'] = $v['h_address'];
					$data['h_required_service_date'] = $v['h_date'];
				}
			}elseif($type=='集港'){
				if($v['h_linkman'] == '@TE_linkman' || $v['h_linkman'] == '@TE_container_man' || $v['h_linkman'] == '@TE_full_container_man' || $v['h_linkman_tel']=='@TE_tel' || $v['h_linkman_tel']=='@TE_container_tel' || $v['h_linkman_tel']=='@TE_full_container_tel' ||
				$v['h_address'] == '@TE_unload' || $v['h_address'] == '@TE_container_depot' || $v['h_address'] == '@TE_full_container_depot' || $v['h_date'] == '@TE_load_time' || $v['h_date'] == '@TE_collect_time'){
					//查数据库
					$sql = "select gt.g_container_man,gt.g_container_tel,gt.g_container_depot,g_container_lng,g_container_lat,gt.g_full_container_man,gt.g_full_container_tel,gt.g_full_container_depot,g_full_container_lng,g_full_container_lat,gt.g_loading_place,gt.g_loading_lng,gt.g_loading_lat,gt.g_loading_man,gt.g_loading_tel,gt.g_packingtime,gt.g_collectiontime from zys_goods_trunk as gt left join zys_waybill_sub_trunk as wst on gt.g_tid= wst.ws_gid where wst.ws_wmtid=".$h_wmtid." GROUP BY ws_gid";
					$row = $db->getrow($sql);
					if($v['h_linkman'] == '@TE_linkman'){
						$data['h_linkman'] = $row['g_loading_man'];
					}elseif($v['h_linkman'] == '@TE_container_man'){

						$data['h_linkman'] = $row['g_container_man'];
					}
					elseif($v['h_linkman'] == '@TE_full_container_man'){

						$data['h_linkman'] = $row['g_full_container_man'];
					}else{
						$data['h_linkman'] = $v['h_linkman'];
					}
					if($v['h_linkman_tel']=='@TE_tel'){
						$data['h_linkman_tel'] = $row['g_loading_tel'];
					}elseif($v['h_linkman_tel']=='@TE_container_tel'){

						$data['h_linkman_tel'] = $row['g_container_tel'];

					}elseif($v['h_linkman_tel']=='@TE_full_container_tel'){

						$data['h_linkman_tel'] = $row['g_full_container_tel'];
					}else{
						$data['h_linkman_tel'] = $v['h_linkman_tel'];
					}
					if($v['h_address'] == '@TE_unload'){
						$data['h_address'] = $row['g_loading_place'];
						$data['h_lng'] = $row['g_loading_lng'];
						$data['h_lat'] = $row['g_loading_lat'];
					}elseif($v['h_address'] == '@TE_container_depot'){
						$data['h_address'] = $row['g_container_depot'];
						$data['h_lng'] = $row['g_container_lng'];
						$data['h_lat'] = $row['g_container_lat'];
					}elseif($v['h_address'] == '@TE_full_container_depot'){
						$data['h_address'] = $row['g_full_container_depot'];
						$data['h_lng'] = $row['g_full_container_lng'];
						$data['h_lat'] = $row['g_full_container_lat'];
					}else{
						$data['h_address'] = $v['h_address'];
					}
					if($v['h_date'] == '@TE_load_time'){
						$data['h_required_service_date'] = $row['g_packingtime'];
					}elseif($v['h_date'] == '@TE_collect_time'){
						$data['h_required_service_date'] = $row['g_collectiontime'];
					}else{
						$data['h_required_service_date'] = $v['h_date'];
					}
				}else{
					$data['h_linkman'] = $v['h_linkman'];
					$data['h_linkman_tel'] = $v['h_linkman_tel'];
					$data['h_address'] = $v['h_address'];
					$data['h_required_service_date'] = $v['h_date'];
				}
			}
			if(!empty($linkman)){
				$data['h_linkman'] =$linkman;
			}
			if(!empty($linkman_tel)){
				$data['h_linkman_tel'] =$linkman_tel;
			}
			if(!empty($address)){
				$data['h_address'] =$address;
			}
			if(!empty($dt)){
				$data['h_required_service_date'] =$dt;
			}
			$data['h_type'] = $v['h_type'];
			$data['h_range'] = intval($v['h_range']);
			$id = $_haul_mod->add($data);

			$sql="select * from zys_haul_define_collect where hc_h_tid=".$v['h_tid'];
			$collect=$db->getAll($sql);
			if($collect){
				foreach ($collect as $c){
					$sql="insert into zys_haul_collect set hc_h_tid='".$id."',
						h_collect_type='".$c['h_collect_type']."',h_collect_des='".$c['h_collect_des']."',h_collect_val='".$c['h_collect_val']."'";
					$db->query($sql);
				}
			}
		}
	}
}
/**
 * 百度消息不错
 * @param unknown_type $channelId
 * @param unknown_type $message
 * @param unknown_type $msg_type
 */
function pushMsg1($channelId,$message,$msg_type){
	$status=0;
	$sdk = new PushSDK();
	$channelId = $channelId?$channelId:'';
	try{
		// 设置消息类型为 通知类型.
		$opts = array (
	    	'msg_type' => $msg_type?$msg_type:0 
		);
		if(empty($channelId)){
			$rs = $sdk -> pushMsgToTag('driver',$message, $opts);
		}else
		// 向目标设备发送一条消息
		$rs = $sdk -> pushMsgToSingleDevice($channelId, $message, $opts);
		// 判断返回值,当发送失败时, $rs的结果为false, 可以通过getError来获得错误信息.
		if($rs === false){
			//print_r($sdk->getLastErrorCode());
			//	print_r($sdk->getLastErrorMsg());
			throw new Exception($sdk->getLastErrorMsg());
		}else{
			// 将打印出消息的id,发送时间等相关信息.
			//print_r($rs);
			$status=1;
			$rnt="发送成功";
		}
	}
	catch (Exception $exp){
		$rnt=$exp->getMessage();
	}
	return JSONResultStr($status,$rnt,null);
}